
import axios from '~/axios'
import qs from 'qs'

export const joinPramas = (url, params) => {
    const index = url.indexOf('?');
    if (index !== -1) {
        url = url.slice(0, index);
    }
    if (JSON.stringify(params) == '{}') return url;
    // Qs.stringify 序列化之后会对一些参数进行编码，这里用decodeURIComponent解码
    return decodeURIComponent(`${url}?${qs.stringify(params)}`);
};


export function login(username, password, loginType) {
    return axios.post('/api/system/login', qs.stringify({
        username,
        password,
        loginType
    }), {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
}
export function login_send_sms(phone) {
    return axios.get('/api/backend/getPhoneCode?loginType=0&phone=' + phone)
}

export function loginPhone(key, phoneCode) {
    return axios.post('/api/backend/checkPhoneCode', {
        key,
        phoneCode
    }, {
        headers: {
            'Content-Type': 'application/json'
        }
    })
}

// 获取用户登录信息
export function getInfo() {
    return axios.post('/api/admin/getinfo')
}

export function logout() {
    return axios.post('/api/system/logout')
}

// 修改密码
export function updatepassword(data) {
    return axios.post('/api/sys/user/changePwd', data)
}

// 重置密码
export function resetpassword(userId) {
    return axios.post('/api/sys/user/initPwd/'+ userId)
}

// 获取审核人list，审核专用接口
export function getApprovalPersonList(page, params = {}) {
    let url = joinPramas('/api/sys/user/approval/list', params)
    return axios.get(url)
}

// 获取申请人list，设备领用专用接口
export function getApplicantUserList(page, params = {}) {
    let url = joinPramas('/api/sys/user/applicant/list', params)
    return axios.get(url)
}

/**
 * 根据ids获取审核人list，审核专用接口
 * @param {*} paramIo 
 */
export function getApprovalPersonByIds(paramIo) {
    return axios({
        url: '/api/sys/user/approval/getListByIds',
        method: 'post',
        data: paramIo
    })
}

export function getManagerList(page, params = {}) {
    let url = joinPramas('/api/sys/user/list', params)
    return axios.get(url)
}

export function getBoatsmanList(page, params = {}) {
    let url = joinPramas('/api/sys/user/boatsman/list', params)
    return axios.get(url)
}


export function updateManagerStatus(id, status) {
    return axios.post(`/api/sys/user/changeStatus/${id}/${status}`)
}

export function createManager(data) {
    return axios.post(`/api/sys/user/add`, data)
}

export function updateManager(id, data) {
    data.id = id
    return axios.post(`/api/sys/user/edit`, data)
}

export function deleteManager(id) {
    return axios.post(`/api/sys/user/delete/${id}`)
}